package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> selectShoppingCartAll(){
        List<ShoppingCart> shoppingCarts = shoppingCartService.list();
        return R.success(shoppingCarts);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long userid = (Long) session.getAttribute("userId");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userid);
        // 判断当前用户点的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){//说明点的是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //判断是否添加过
        //根据菜品/套餐id查询
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one == null){//说明是第一次添加到购物车
            shoppingCart.setUserId(userid);
            shoppingCartService.save(shoppingCart);//number=1
            return R.success(shoppingCart);
        }else {//已经加过，修改过后重新添加
            one.setNumber(one.getNumber() + 1);//number+1
            shoppingCartService.updateById(one);//更新修改过的数据
            return R.success(one);
        }
    }

    /**
     * 取消添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        Long userid = BaseContext.getCurrentId();
        lqw.eq(ShoppingCart::getUserId,userid);
        // 判断当前用户点的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){//是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {//是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number > 0){//多个菜品/套餐
            shoppingCartService.updateById(one);
        }else {//  一个菜品/套餐
            shoppingCartService.removeById(one.getId());
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> deleteShoppingCartAll(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userid);
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }
}
